package com.rw.engine.frequency.anno;

import com.rw.engine.frequency.interceptor.DefaultFrequencyFilterRule;
import com.rw.engine.frequency.interceptor.FrequencyFilterRule;
import com.rw.engine.CommonLimit;
import org.springframework.core.annotation.AliasFor;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.TimeUnit;

/**
 * 频率限制器
 */
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@CommonLimit
public @interface FrequencyLimit {
    /**
     * 在一定时间内，访问的次数不能超过一个值
     * requestsLimit代表了这个值
     */
    int requestsLimit() default 5;

    /**
     * 在指定时间内，访问次数不能太多
     * requestsLimitTime指定时间，
     */
    long requestsLimitTime() default 3;

    /**
     * 限制时间的时间单位
     */
    TimeUnit requestsLimitTimeUnit() default TimeUnit.SECONDS;

    /**
     * 两次访问最小间隔时间
     */
    long minInterval() default 100;

    /**
     * 两次访问最小间隔的单位
     */
    TimeUnit minIntervalUtil() default TimeUnit.MILLISECONDS;

    @AliasFor(annotation = CommonLimit.class, attribute = "filterRule")
    Class<? extends FrequencyFilterRule>[] frequencyFilterRule() default DefaultFrequencyFilterRule.class;
}
